package cn.itlight.controller;

import cn.itlight.block.UserBlock;
import cn.itlight.domain.User;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope  //刷新配置
@RestController
public class UserController {

    @Value("${test}")
    private String test;

    @GetMapping("/user/{id}")
    //限流降级
    @SentinelResource( value="user", blockHandler="exceptionHandler", blockHandlerClass = UserBlock.class, fallback="fallback")
    public User get(@PathVariable Long id){
        int a = 1/0;
        return new User(id,"zs:"+id, "我是zs" + test);
    }

    // 限流与阻塞处理 : 参数要和 被降级的方法参数一样
    public User fallback(@PathVariable Long id, Throwable ex) {
        ex.printStackTrace();
        System.out.println("限流了...");
        return new User(-2L,"降级了","降级了");
    }



}
